﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.Model;
using rock.DAL;
using rock.UI.common;

namespace rock.UI.houseinfo
{
    public partial class FormRoomThingUpdate : Form
    {
        ModelHOUSE_THING modelHOUSE_THING = new ModelHOUSE_THING();
        public ModelHOUSE_THING _ModelHOUSE_THING { get {return modelHOUSE_THING;} set{modelHOUSE_THING = value;} }

        DalHOUSE_THING dalHOUSE_THING = new DalHOUSE_THING();
        DalCFG_CODE dalCFG_CODE = new DalCFG_CODE();
        public FormRoomThingUpdate()
        {
            InitializeComponent();
            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;
        }

        private void FormRoomThingUpdate_Load(object sender, EventArgs e)
        {
          
            this.textBox2SIZE.Text = this._ModelHOUSE_THING.SIZE;
            this.textBox3COUNT.Text = this._ModelHOUSE_THING.COUNT+"";
            this.textBox4PRICE.Text = this._ModelHOUSE_THING.PRICE + "";
            this.textBox5TATLE_PRICE.Text = this._ModelHOUSE_THING.TOTLE_PRICE + "";
            this.textBox6NOTE.Text = this._ModelHOUSE_THING.NOTE;


            DataSet deviceSet = this.dalCFG_CODE.GetList("TYPE = '设备'");
            this.comboBox1NAME.DisplayMember = "CODE_VALUE";
            this.comboBox1NAME.ValueMember = "CODE_VALUE";
            this.comboBox1NAME.DataSource = deviceSet.Tables[0];
            if (this.modelHOUSE_THING.NAME != null && !this._ModelHOUSE_THING.NAME.Equals(""))
            {
                this.comboBox1NAME.SelectedValue = this.modelHOUSE_THING.NAME + "";
            }

        }

        private Boolean check()
        {
            try
            {
                StringUtils.checkText(this.textBox2SIZE.Text, 0, 30, "规格");
                StringUtils.checkIsPositiveInteger(this.textBox3COUNT.Text, "数量");
                StringUtils.checkIsDecimal(this.textBox4PRICE.Text, "单价");
                StringUtils.checkIsDecimal(this.textBox5TATLE_PRICE.Text, "总价");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            return true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            }


            this._ModelHOUSE_THING.NAME = (string)this.comboBox1NAME.SelectedValue;
            this._ModelHOUSE_THING.SIZE = this.textBox2SIZE.Text;
            this._ModelHOUSE_THING.COUNT = Convert.ToInt32(this.textBox3COUNT.Text);
            this._ModelHOUSE_THING.PRICE = Convert.ToDecimal(this.textBox4PRICE.Text);
            this._ModelHOUSE_THING.TOTLE_PRICE = Convert.ToDecimal(this.textBox5TATLE_PRICE.Text);
            this._ModelHOUSE_THING.NOTE = this.textBox6NOTE.Text;


            if (dalHOUSE_THING.Exists(this._ModelHOUSE_THING.SEQID))
            {
                dalHOUSE_THING.Update(this._ModelHOUSE_THING);
            }
            else
            {
                dalHOUSE_THING.Add(this._ModelHOUSE_THING);
            }
            //触发代理
            oneRefreshParentData();
            this.Close();
        }

        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //代理
        public delegate void RefreshParentData();
        public RefreshParentData oneRefreshParentData;

        private void button3_Click(object sender, EventArgs e)
        {
            try{
                StringUtils.checkIsPositiveInteger(this.textBox3COUNT.Text, "数量");
                StringUtils.checkIsDecimal(this.textBox4PRICE.Text, "单价");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
                return ;
            }
            this.textBox5TATLE_PRICE.Text = (int.Parse(this.textBox3COUNT.Text) * decimal.Parse(this.textBox4PRICE.Text) )+ "";
        }
    }
}
